#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 万进制   个位对齐
#define MAXSIZE 100 // 最大位数100位
typedef struct num
{
    int val;
    struct num *next, *pre;
};

// 对于0值的处理 怎么解决？
num *creat()
{
    num *list = (num *)malloc(sizeof(num));
    list->next = list->pre = list;
    list->val = 0;
    char buf[MAXSIZE] = {'\0'};
    int k = -1;
    // 数据的输入
    char c = getchar();
    while (c >= '0' && c <= '9')
    {
        buf[++k] = c;
        c = getchar();
    }
    for (int i = 0; i <= k / 2; i++)
    {
        char c = buf[i];
        buf[i] = buf[k - i];
        buf[k - i] = c;
    }
    int ii = 0;
    while (ii <= k)
    {
        num *node = (num *)malloc(sizeof(num));
        node->val = 0;
        node->pre = node->next = NULL;
        // bug 以0结尾的数字无法获取
        for (int i = 0; i < 4 && i + ii <= k; i++)
        {
            int n = buf[i + ii] - '0';
            node->val *= 10;
            node->val += n;
        }
        ii += 4;
        list->next->pre = node;
        node->pre = list;
        node->next = list->next;
        list->next = node;
    }
    return list;
}
void print_num(num *list)
{
    num *p = list->next;
    while (p != list)
    {
        if (p->val == 0)
            printf("000");
        printf("%d", p->val);
        if (p->next != list)
            printf(",");
        p = p->next;
    }
    printf("\n");
}
num *add(num *A, num *B)
{
    num *sum = (num *)malloc(sizeof(num));
    sum->val = 0;
    sum->next = sum->pre = sum;
    num *m, *n;
    m = A->pre, n = B->pre;
    int t = 0;
    while (m != A && n != B)
    {
        t += (m->val + n->val);
        num *node = (num *)malloc(sizeof(num));
        node->val = t % 10000;
        t /= 10000;
        sum->next->pre = node;
        node->pre = sum;
        node->next = sum->next;
        sum->next = node;
        m = m->pre;
        n = n->pre;
    }
    num *p, *head;
    p = m;
    head = A;
    if (n != B)
    {
        p = n;
        head = B;
    }
    while (p != head)
    {
        t += p->val;
        num *node = (num *)malloc(sizeof(num));
        node->val = t % 10000;
        t /= 10000;
        sum->next->pre = node;
        node->pre = sum;
        node->next = sum->next;
        sum->next = node;
        p = p->pre;
    }
    if (t != 0)
    {
        num *node = (num *)malloc(sizeof(num));
        node->val = t % 10000;
        t /= 10000;
        sum->next->pre = node;
        node->pre = sum;
        node->next = sum->next;
        sum->next = node;
    }
    return sum;
}

int main()
{
    /*******************code*******************/
    num *A = creat();
    print_num(A);
    num *B = creat();
    print_num(B);
    num *sum = add(A, B);
    print_num(sum);
    fflush(stdin);
    /******************************************/
    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
